package com.chen.palmar.project.set;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chen.palmar.R;
import com.chen.palmar.base.BaseActivity;
import com.chen.palmar.common.constant.AppConstant;
import com.chen.palmar.common.datasource.DataCenter;
import com.chen.palmar.common.override.HttpSubscriber;
import com.chen.palmar.common.widget.view.AutoTabLayout;
import com.chen.palmar.common.widget.view.AutoToolbar;
import com.chen.palmar.common.widget.view.CircleImageView;
import com.chen.palmar.entity.CollectionAgencyEntity;
import com.chen.palmar.entity.CollectionInfoEntity;
import com.chen.palmar.entity.CollectionListManager;
import com.chen.palmar.entity.CollectionProducerEntity;
import com.chen.palmar.entity.CollectionStoreEntity;
import com.chen.palmar.project.agency.AgencyDetailActivity;
import com.chen.palmar.project.home.InfoDetailActivity;
import com.chen.palmar.project.producer.ProducerDetailActivity;
import com.chen.palmar.project.producer.StoreDetailActivity;
import com.primb.androidlibs.ui.recyclerview.BaseQuickAdapter;
import com.primb.androidlibs.ui.recyclerview.BaseViewHolder;
import com.squareup.picasso.Picasso;

import java.util.Arrays;

import butterknife.Bind;
import rx.Observable;
import rx.Subscription;
import rx.functions.Func4;

/**
 * @author Chen
 * @date 2018/1/9
 * 功能描述：
 */

public class CollectionListActivity extends BaseActivity {
    @Bind(R.id.title_bar)
    TextView titleBar;
    @Bind(R.id.tool_bar)
    AutoToolbar toolBar;
    @Bind(R.id.rv_collection_list)
    RecyclerView rvCollectionList;
    @Bind(R.id.tab_detail)
    AutoTabLayout tabDetail;
    private CollectionListManager collectionListManager;
    private BaseQuickAdapter<CollectionInfoEntity.DataBean, BaseViewHolder> adapterInfo;
    private BaseQuickAdapter<CollectionStoreEntity.DataBean, BaseViewHolder> adapterStore;
    private BaseQuickAdapter<CollectionProducerEntity.DataBean, BaseViewHolder> adapterProducer;
    private BaseQuickAdapter<CollectionAgencyEntity.DataBean, BaseViewHolder> adapterAgency;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    public void init() {
        toolBar.setNavigationOnClickListener(v -> CollectionListActivity.this.finish());
        titleBar.setText("收藏管理");

        tabDetail.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                setDataToView(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        try {
            getListData();
        } catch (Exception e) {
            e.printStackTrace();
            showToast("服务器数据异常");
        }

        rvCollectionList.setItemAnimator(new DefaultItemAnimator());
        adapterInfo = new BaseQuickAdapter<CollectionInfoEntity.DataBean, BaseViewHolder>(R.layout.item_home) {
            @Override
            protected void convert(BaseViewHolder helper, CollectionInfoEntity.DataBean item) {
                helper.setText(R.id.tv_item_home_content, item.getContent());
                helper.setText(R.id.tv_item_home_good, item.getPraise_nums() + "");
                helper.setText(R.id.tv_item_home_watch, item.getBrowse_nums() + "");
                helper.setText(R.id.tv_item_home_location, item.getAddress_detail());
                helper.setText(R.id.tv_item_home_name, item.getUser_name());
                helper.setText(R.id.tv_item_date, item.getCreate_at());
                helper.setText(R.id.tv_item_home_type, "1".equals(item.getType()) ? "供应" : "采购");

                CircleImageView head = (CircleImageView) helper.getView(R.id.iv_item_home_head);
                Picasso.with(CollectionListActivity.this).load(item.getUser_avatar()).placeholder(R.mipmap.home_item_head_icon).error(R.mipmap.home_item_head_icon).into(head);
                RecyclerView rvImg = (RecyclerView) helper.getView(R.id.rv_item_home_img);
                rvImg.setNestedScrollingEnabled(false);
                GridLayoutManager layoutManager = new GridLayoutManager(CollectionListActivity.this, 3);
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                rvImg.setLayoutManager(layoutManager);
                BaseQuickAdapter<String, BaseViewHolder> adapterImg = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_home_img) {
                    @Override
                    protected void convert(BaseViewHolder helper, String item) {
                        ImageView view = (ImageView) helper.getView(R.id.iv_item_img);
                        Picasso.with(CollectionListActivity.this).load(item).resize(AppConstant.SCREEN_WIDTH / 3, AppConstant.SCREEN_WIDTH / 3).placeholder(R.mipmap.occupy_icon).error(R.mipmap.occupy_icon).into(view);
                    }
                };
                rvImg.setItemAnimator(new DefaultItemAnimator());
                adapterImg.setNewData(item.getImgList());
                rvImg.setAdapter(adapterImg);
            }
        };
        adapterInfo.setOnItemClickListener((adapter1, view, position) -> {
            Intent intent = new Intent(CollectionListActivity.this, InfoDetailActivity.class);
            intent.putExtra("info", adapterInfo.getData().get(position).getArticleId() + "");
            startActivity(intent);
        });

        adapterAgency = new BaseQuickAdapter<CollectionAgencyEntity.DataBean, BaseViewHolder>(R.layout.item_agency) {
            @Override
            protected void convert(BaseViewHolder helper, CollectionAgencyEntity.DataBean item) {
                helper.setText(R.id.tv_item_agency_name, item.getName());
                helper.setText(R.id.tv_item_agency_content, item.getDesc() + "");
                helper.setText(R.id.tv_item_agency_brand, "经营品牌：" + (TextUtils.isEmpty(item.getBrand()) ? "" : item.getBrand()));
                helper.setText(R.id.tv_item_agency_location, item.getAddress_name());
                helper.setText(R.id.tv_item_agency_distance, "");

                ImageView head = (ImageView) helper.getView(R.id.iv_item_agency);
                Picasso.with(CollectionListActivity.this).load(item.getLogo()).placeholder(R.mipmap.home_item_head_icon).error(R.mipmap.home_item_head_icon).into(head);

                RecyclerView rvTag = (RecyclerView) helper.getView(R.id.rv_item_agency_tag);
                if (TextUtils.isEmpty(item.getChannel())) {
                    rvTag.setVisibility(View.GONE);
                } else {
                    rvTag.setVisibility(View.VISIBLE);
                    rvTag.setNestedScrollingEnabled(false);
                    LinearLayoutManager layoutManager = new LinearLayoutManager(CollectionListActivity.this);
                    layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                    rvTag.setLayoutManager(layoutManager);
                    BaseQuickAdapter<String, BaseViewHolder> adapterImg = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_agency_tag) {
                        @Override
                        protected void convert(BaseViewHolder helper, String item) {
                            helper.setText(R.id.iv_item_tag, item);
                        }
                    };
                    rvTag.setItemAnimator(new DefaultItemAnimator());
                    String[] split = item.getChannel().split(" ");
                    adapterImg.setNewData(Arrays.asList(split));
                    rvTag.setAdapter(adapterImg);
                }
            }
        };
        adapterAgency.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(CollectionListActivity.this, AgencyDetailActivity.class);
            intent.putExtra("info", ((CollectionAgencyEntity.DataBean) adapter.getData().get(position)).getDistrId() + "");
            startActivity(intent);
        });

        adapterStore = new BaseQuickAdapter<CollectionStoreEntity.DataBean, BaseViewHolder>(R.layout.item_producer_store) {
            @Override
            protected void convert(BaseViewHolder helper, CollectionStoreEntity.DataBean item) {
                helper.setText(R.id.tv_item_new_name, item.getTitle());
                helper.setText(R.id.tv_item_new_price, item.getGuidance_price() + "元");
                ImageView view = (ImageView) helper.getView(R.id.iv_item_new);
                Picasso.with(CollectionListActivity.this).load(item.getImg()).resize(AppConstant.SCREEN_WIDTH / 2 - 50, AppConstant.SCREEN_WIDTH / 2 - 80).placeholder(R.mipmap.occupy_icon).error(R.mipmap.occupy_icon).into(view);
            }
        };
        adapterStore.setOnItemClickListener((adapter1, view, position) -> {
            Intent intent = new Intent(CollectionListActivity.this, StoreDetailActivity.class);
            intent.putExtra("info", adapterStore.getData().get(position).getProductId() + "");
            startActivity(intent);
        });

        adapterProducer = new BaseQuickAdapter<CollectionProducerEntity.DataBean, BaseViewHolder>(R.layout.item_type) {
            @Override
            protected void convert(BaseViewHolder helper, CollectionProducerEntity.DataBean item) {
                helper.setText(R.id.tv_item_type_content, item.getDesc());
                helper.setText(R.id.tv_item_type_name, item.getName());
                CircleImageView head = (CircleImageView) helper.getView(R.id.iv_item_type_head);
                Picasso.with(CollectionListActivity.this).load(item.getLogo()).placeholder(R.mipmap.home_item_head_icon).error(R.mipmap.home_item_head_icon).into(head);
            }
        };
        adapterProducer.setOnItemClickListener((adapter1, view, position) -> {
            Intent intent = new Intent(CollectionListActivity.this, ProducerDetailActivity.class);
            intent.putExtra("info", adapterProducer.getData().get(position).getFactoryId() + "");
            startActivity(intent);
        });
    }

    private void setDataToView(int index) {
        if (index == 2) {
            GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            rvCollectionList.setLayoutManager(layoutManager);
        } else {
            LinearLayoutManager layoutManager = new LinearLayoutManager(this);
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            rvCollectionList.setLayoutManager(layoutManager);
        }

        switch (index) {
            case 0:
                rvCollectionList.setAdapter(adapterInfo);
                adapterInfo.setNewData(collectionListManager.getInfoEntity().getData());
                break;
            case 1:
                rvCollectionList.setAdapter(adapterAgency);
                adapterAgency.setNewData(collectionListManager.getAgencyEntity().getData());
                break;
            case 2:
                rvCollectionList.setAdapter(adapterStore);
                adapterStore.setNewData(collectionListManager.getStoreEntity().getData());
                break;
            case 3:
                rvCollectionList.setAdapter(adapterProducer);
                adapterProducer.setNewData(collectionListManager.getProducerEntity().getData());
                break;
        }
    }

    private void getListData() {
        Observable<CollectionInfoEntity> collectionInfoList = DataCenter.collectionInfoList();
        Observable<CollectionAgencyEntity> collectionAgencyList = DataCenter.collectionAgencyList();
        Observable<CollectionStoreEntity> collectionStoreList = DataCenter.collectionStoreList();
        Observable<CollectionProducerEntity> collectionProducerList = DataCenter.collectionProducerList();

        Subscription subscribe = Observable.zip(collectionInfoList, collectionAgencyList, collectionStoreList, collectionProducerList, new Func4<CollectionInfoEntity, CollectionAgencyEntity, CollectionStoreEntity, CollectionProducerEntity, CollectionListManager>() {
            @Override
            public CollectionListManager call(CollectionInfoEntity collectionInfoEntity, CollectionAgencyEntity collectionAgencyEntity, CollectionStoreEntity collectionStoreEntity, CollectionProducerEntity collectionProducerEntity) {
                CollectionListManager manager = new CollectionListManager(collectionInfoEntity, collectionAgencyEntity, collectionStoreEntity, collectionProducerEntity);
                return manager;
            }
        }).subscribe(new HttpSubscriber<CollectionListManager>(this, showLoading()) {

            @Override
            public void onNext(CollectionListManager collectionListManager) {
                CollectionListActivity.this.collectionListManager = collectionListManager;
                tabDetail.getTabAt(0).select();
                setDataToView(0);
            }
        });
        subscription.add(subscribe);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_collection_list;
    }
}
